﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AirCondition.Data;
using System.Data.SqlClient;
using System.IO;
using AirCondition.Data;

namespace AirCondition
{
    public partial class _Default : System.Web.UI.Page
    {



        protected void Page_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 提交新增员工信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Unnamed1_Click(object sender, EventArgs e)
        {
           
            string sql = "insert into tb_Eemployeeinfo(ename,egender,ebirth,enativeplace,eidentification,estatus,ecellphone,etelephone,eaddress,edepartment,eposition,ejobtype,esalary,ecardname,esalarycard,esalarycardbank,ecardpoint,ecardownerrelationship,ephoto,eidscanner) values(@ename,@egender,@ebirth,@enativeplace,@eidentification,@estatus,@ecellphone,@etelephone,@eaddress,@edepartment,@eposition,@ejobtype,@esalary,@ecardname,@esalarycard,@esalarycardbank,@ecardpoint,@ecardownerrelationship,@ephoto,@eidscanner)";

            SqlParameter[] sqlPara = { new SqlParameter("@ename", SqlDbType.NVarChar),
                                       new SqlParameter("@egender", SqlDbType.NVarChar), 
                                       new SqlParameter("@ebirth", SqlDbType.VarChar), 
                                       new SqlParameter("@enativeplace", SqlDbType.NVarChar),
                                       new SqlParameter("@eidentification", SqlDbType.VarChar), 
                                       new SqlParameter("@estatus", SqlDbType.NVarChar),
                                       new SqlParameter("@ecellphone", SqlDbType.VarChar),
                                       new SqlParameter("@etelephone", SqlDbType.VarChar), 
                                       new SqlParameter("@eaddress", SqlDbType.NVarChar),
                                       new SqlParameter("@edepartment", SqlDbType.NVarChar), 
                                       new SqlParameter("@eposition", SqlDbType.NVarChar), 
                                       new SqlParameter("@ejobtype", SqlDbType.NVarChar),
                                       new SqlParameter("@esalary", SqlDbType.VarChar), 
                                       new SqlParameter("@ecardname", SqlDbType.NVarChar),
                                       new SqlParameter("@esalarycard", SqlDbType.VarChar),
                                       new SqlParameter("@esalarycardbank", SqlDbType.NVarChar), 
                                       new SqlParameter("@ecardpoint", SqlDbType.NVarChar),       
                                       new SqlParameter("@ecardownerrelationship", SqlDbType.NVarChar),
                                       new SqlParameter("@ephoto", SqlDbType.Image),
                                       new SqlParameter("@eidscanner", SqlDbType.Image),                             
                                     };

            sqlPara[0].Value = TextBox1.Text.Trim();
            sqlPara[1].Value = DropDownList1.SelectedItem.Text.Trim();
            sqlPara[2].Value = TextBox2.Text.Trim();
            sqlPara[3].Value = TextBox5.Text.Trim();
            sqlPara[4].Value = TextBox7.Text.Trim();

            sqlPara[5].Value = TextBox3.Text.Trim();
            sqlPara[6].Value = TextBox4.Text.Trim();
            sqlPara[7].Value = TextBox6.Text.Trim();
            sqlPara[8].Value = DropDownList2.SelectedItem.Text.Trim();
            sqlPara[9].Value = TextBox8.Text.Trim();


            sqlPara[10].Value = TextBox9.Text.Trim();
            sqlPara[11].Value = DropDownList3.SelectedItem.Text.Trim();
            sqlPara[12].Value = TextBox11.Text.Trim();

            sqlPara[13].Value = TextBox15.Text.Trim();
            sqlPara[14].Value = TextBox12.Text.Trim();
            sqlPara[15].Value = TextBox13.Text.Trim();

            sqlPara[16].Value = TextBox14.Text.Trim();
            sqlPara[17].Value = TextBox16.Text.Trim();

            sqlPara[18].Value = Data.PhotoRW.PhotoToArray(SaveFile(FileUpload1));
            sqlPara[19].Value = Data.PhotoRW.PhotoToArray(SaveFile(FileUpload2));

            int rows = new SQLHelp().ExecuteNonQuery(sql, sqlPara, ConfigurationManager.AppSettings["SQLServer"]);
            //int rows = ExecuteNonQuery(sql, sqlPara, ConfigurationManager.AppSettings["SQLExpress"]);

            Response.Write("<script language='javascript'>alert('添加成功');</script>");

        }



        //把二进制的图片插到数据库
        private void Save(byte[] image)
        {
            string sql = "insert into table2(aaa,photo) values(@aaa,@photo)";
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@aaa", SqlDbType.Int);
            param[0].Value = 1;
            param[1] = new SqlParameter("@photo", SqlDbType.Image);
            param[1].Value = image;

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[0];

            SqlCommand commd = new SqlCommand(sql, conn);
            commd.Parameters.Add(param[0]);
            commd.Parameters.Add(param[1]);

            try
            {
                conn.Open();
                commd.ExecuteNonQuery();
                //MessageBox.Show("把图片成功的插入数据库");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }


      



   


    

        private string SaveFile(FileUpload up)
        {
            //string file = "~/uploadfiles/";
            string file = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString())+"uploadfiles\\";

            if (!System.IO.Directory.Exists(file))
            {
                System.IO.Directory.CreateDirectory(file);
            }
            file = file + up.FileName;
            up.SaveAs(file);
            //return file + up.FileName;
            return file ;
        }



        //本地路径转换成URL相对路径
        private string UrlConvertor(string imagesurl1)
        {

            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录

            string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径

            imagesurl2 = imagesurl2.Replace(@"\", @"/");

            //imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");

            return imagesurl2;

        }

        //相对路径转换成服务器本地物理路径
        private string UrlConvertorLocal(string imagesurl1)
        {

            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录

            string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"\"); //转换成绝对路径

            return imagesurl2;

        }


       
    }
}
